package database;

import java.sql.CallableStatement;

import object.Employee;
import utility.StringUtil;
import clientMain.Login;

public class EmployeeDelegate {
	
	public boolean insertEmployee(Employee em, int userInsert){
		boolean flag = true;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_InsertEmployee(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			cst.setString(1, StringUtil.setFilterIllegal(em.getEmFirstName()));
			cst.setString(2, StringUtil.setFilterIllegal(em.getEmLastName()));
			cst.setString(3, StringUtil.setFilterIllegal(em.getEmFullName()));
			cst.setString(4, StringUtil.setFilterIllegal(em.getEmBirthDay()));
			cst.setBoolean(5, em.isEmSex());
			cst.setString(6, StringUtil.setFilterIllegal(em.getEmCMTND()));
			cst.setString(7, StringUtil.setFilterIllegal(em.getEmHomeTownAdd()));
			cst.setInt(8, em.getEmHomeTown());
			cst.setString(9, StringUtil.setFilterIllegal(em.getEmCurrentAdd()));
			cst.setInt(10, em.getEmCurrentCity());
			cst.setString(11, StringUtil.setFilterIllegal(em.getEmMobile()));
			cst.setString(12, StringUtil.setFilterIllegal(em.getEmPhone()));
			cst.setString(13, StringUtil.setFilterIllegal(em.getEmEmail()));
			cst.setInt(14, em.getEmContract());
			cst.setInt(15, em.getEmDiplomas());
			cst.setInt(16, em.getEmSpecialized());
			cst.setString(17, StringUtil.setFilterIllegal(em.getEmImage()));
			cst.setString(18, StringUtil.setFilterIllegal(em.getEmDateStart()));
			cst.setInt(19, em.getEmPos());
			cst.setInt(20, em.getEmDep());
			cst.setInt(21, em.getEmEmT());
			cst.setInt(22, userInsert);
			cst.executeUpdate();
			Login.conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	
	public boolean updateEmployee(Employee em, int userUpdate){
		boolean flag = true;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_UpdateEm(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			cst.setString(1, em.getEmId());
			cst.setString(2, StringUtil.setFilterIllegal(em.getEmFirstName()));
			cst.setString(3, StringUtil.setFilterIllegal(em.getEmLastName()));
			cst.setString(4, StringUtil.setFilterIllegal(em.getEmFullName()));
			cst.setString(5, StringUtil.setFilterIllegal(em.getEmBirthDay()));
			cst.setBoolean(6, em.isEmSex());
			cst.setString(7, StringUtil.setFilterIllegal(em.getEmCMTND()));
			cst.setString(8, StringUtil.setFilterIllegal(em.getEmHomeTownAdd()));
			cst.setInt(9, em.getEmHomeTown());
			cst.setString(10, StringUtil.setFilterIllegal(em.getEmCurrentAdd()));
			cst.setInt(11, em.getEmCurrentCity());
			cst.setString(12, StringUtil.setFilterIllegal(em.getEmMobile()));
			cst.setString(13, StringUtil.setFilterIllegal(em.getEmPhone()));
			cst.setString(14, StringUtil.setFilterIllegal(em.getEmEmail()));
			cst.setInt(15, em.getEmContract());
			cst.setInt(16, em.getEmDiplomas());
			cst.setInt(17, em.getEmSpecialized());
			cst.setString(18, StringUtil.setFilterIllegal(em.getEmImage()));
			cst.setString(19, StringUtil.setFilterIllegal(em.getEmDateStart()));
			cst.setString(20, StringUtil.setFilterIllegal(em.getEmDateEnd()));
			cst.setInt(21, em.getEmPos());
			cst.setInt(22, em.getEmDep());
			cst.setInt(23, em.getEmEmT());
			cst.setInt(24, userUpdate);
			cst.executeUpdate();
			Login.conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	
	
}


